/**
 * @(#)TimeUtil.java, 2013-2-14. Copyright 2013 RENREN, Inc. All rights
 *                    reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is subject
 *                    to license terms.
 */
package com.wennimei.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shichen.feng@renren-inc.com
 */
public class TimeUtil {

    public static Date findDate(String content) {
        if (content.matches(".*[今天|今日].*"))
            return new Date();
        else if (content.matches(".*[明天|明日].*"))
            return getDayBeforeAfter(new Date(), 1);
        else if (content.matches(".*[后天].*"))
            return getDayBeforeAfter(new Date(), 2);

        Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return parseDate(matcher.group(), null);
        }

        Pattern pattern2 = Pattern.compile("\\d{2}-\\d{2}");
        Matcher matcher2 = pattern2.matcher(content);
        while (matcher2.find()) {
            return parseDate(
                    formatDate(new Date(), "yyyy") + "-" + matcher2.group(),
                    null);
        }
        return new Date();
    }

    private static final long ONE_DAY_MILES = 24 * 60 * 60 * 1000;

    /**
     * 获取两日期之间的间隔天数
     */
    public static long getNumBetweenDays(Date date1, Date date2) {
        long expire = date1.getTime() - date2.getTime();
        return Math.abs(expire) / ONE_DAY_MILES;
    }

    /**
     * 获取较小的一天的毫秒数
     */
    public static long getMinDay(Date date1, Date date2) {
        long expire = date1.getTime() - date2.getTime();
        return expire < 0L ? date1.getTime() : date2.getTime();
    }

    /**
     * 获取给定日期n天之前或之后的日期
     * 
     * @param date
     *            给定日期
     * @param day
     *            可为正负，正值代表n天之后的日期，负值代表n天之前的日期
     */
    public static Date getDayBeforeAfter(Date date, int n) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, n);
        return c.getTime();
    }

    /**
     * 获取给定日期00:00:00的时间
     */
    public static Date getPureDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 格式化日期,默认为yyyy-MM-dd
     * 
     * @param date
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        if (null == pattern)
            pattern = "yyyy-MM-dd";
        if (null == date)
            date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String formatTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDate(Date date) {
        return formatDate(date, null);
    }

    /**
     * 判断给定两个日期string型，前者是否早于后者
     * 
     * @param date1
     * @param date2
     * @return 若date1早于date2则返回1，若晚于则返回-1，若相等则返回0,若转换异常返回2
     */
    public static int isDateBeforeOrAfter(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date _date1;
        Date _date2;
        try {
            _date1 = sdf.parse(date1);
            _date2 = sdf.parse(date2);
        } catch (Exception e) {
            return -2;
        }
        long flag = _date1.getTime() - _date2.getTime();
        return flag == 0 ? 0 : flag < 0 ? 1 : -1;
    }

    public static int isDateBeforeOrAfter(Date date1, Date date2) {
        long flag = date1.getTime() - date2.getTime();
        return flag == 0 ? 0 : flag < 0 ? 1 : -1;
    }

    /**
     * 根据给定日期获得年月的字符串
     * 
     * @param date
     *            日期
     * @param month
     *            需要加减的月份数
     * @return 例如传入2012-07-07,-1 返回201206
     */
    public static String getMonthAndDateStr(Date date, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(new Date(c.getTime().getTime()));
    }

    /**
     * 根据给定日期获得年的字符串
     * 
     * @param date
     *            日期
     * @return 例如传入2012-07-07,-1 返回20120707
     */
    public static String getDateStr(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date(c.getTime().getTime()));
    }

    /**
     * 获取给定日期如“201206”的月份的最后一天的日期
     * 
     * @param date
     *            例:201206
     * @return 例：2012-06-30
     */
    public static String getLastDay(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(c.getTime());
    }

    /**
     * 转换日期
     * 
     * @param date
     * @param pattern
     *            若为null 默认为“yyyy-MM-dd”
     * @return
     */
    public static Date parseDate(String date, String pattern) {
        if (null == pattern)
            pattern = "yyyy-MM-dd";

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date _date = null;

        try {
            _date = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return _date;
    }

    /**
     * 获取给定日期的星期数
     * 
     * @param date
     * @return 星期日为1 类推
     */
    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取给定日期以及小时，分，秒的时间
     */
    public static Date getPureDate(Date date, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.SECOND, second);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
}
